<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace api\user\controller;

use app\user\model\UserModel;
use cmf\controller\HomeBaseController;
use think\Validate;

class RegisterController extends HomeBaseController {

	/**
	 * 前台用户注册
	 */
	public function index() {
		$redirect = $this->request->post("redirect");
		if (empty($redirect)) {
			$redirect = $this->request->server('HTTP_REFERER');
		} else {
			$redirect = base64_decode($redirect);
		}
		session('login_http_referer', $redirect);

		if (cmf_is_user_login()) {
			return redirect($this->request->root() . '/');
		} else {
			return $this->fetch(":register");
		}
	}

	/**
	 * 前台用户注册提交
	 */
	public function doRegister() {
		if ($this->request->isPost()) {
			$rules = [
				// 'captcha' => 'require',
				'mobile' => 'require',
				'code' => 'require',
				'password' => 'require|min:6|max:32',

			];

			$isOpenRegistration = cmf_is_open_registration();

			if ($isOpenRegistration) {
				unset($rules['code']);
			}

			// $captchaId = empty($data['_captcha_id']) ? '' : $data['_captcha_id'];
			// if (!cmf_captcha_check($data['captcha'], $captchaId)) {
			// $this->error('验证码错误');
			// }

			$data = $this->request->post();
			$register = new UserModel();
			// if (Validate::is($data['username'], 'email')) {
			// $user['user_email'] = $data['username'];
			// $log = $register->registerEmail($user);
			// } else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
			// $user['mobile'] = $data['username'];
			// $log = $register->registerMobile($user);
			// } else {
			// $log = 2;
			// }
			if ($data['reg_type'] == 1) {
				$user['avatar'] = $data['avatarUrl'];
				$user['user_nickname'] = $data['nickName'];
				$user['sex'] = $data['gender'];
				$user['province'] = $data['province'];
				$user['city'] = $data['city'];
				$user['country'] = $data['country'];
				$user['openid'] = $data['openid'];
				$log = $register->registerWx($user);
			} elseif ($data['reg_type'] == 2 && preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['mobile'])) {

				$validate = new Validate($rules);
				$validate->message([
					'code.require' => '验证码不能为空',
					'mobile.require' => '手机号不能为空',
					'password.require' => '密码不能为空',
					'password.max' => '密码不能超过32个字符',
					'password.min' => '密码不能小于6个字符',
					// 'captcha.require' => '验证码不能为空',
				]);

				if (!$validate->check($data)) {
					return ['status' => false, 'msg' => $validate->getError()];
				}

				if (!$isOpenRegistration) {
					$errMsg = cmf_check_verification_code($data['mobile'], $data['code']);
					if (!empty($errMsg)) {
						return ['status' => false, 'msg' => $errMsg];
					}
				}
				$user['mobile'] = $data['mobile'];
				// $user['user_nickname'] = $data['user_nickname'];
				$user['user_pass'] = $data['password'];
				$log = $register->registerMobile($user);
			} else {
				$log = ['status' => false, 'msg' => '注册失败'];
			}
			$sessionLoginHttpReferer = session('login_http_referer');
			$redirect = empty($sessionLoginHttpReferer) ? cmf_get_root() . '/' : $sessionLoginHttpReferer;

			return $log;
		} else {
			return ['status' => false, 'msg' => '注册失败'];
		}
	}

	//更新用户微信信息
	public function updatewxinfo() {
		if ($this->request->isPost()) {
			$register = new UserModel();
			$data = $this->request->post();
			$user['avatar'] = $data['avatarUrl'];
			$user['user_nickname'] = $data['nickName'];
			$user['sex'] = $data['gender'];
			$user['province'] = $data['province'];
			$user['city'] = $data['city'];
			$user['country'] = $data['country'];
			$user['openid'] = $data['openid'];
			$user['userid'] = $data['userid'];
			$user['unionid'] = $data['unionid'];
			$log = $register->updatewxinfo($user);
			return json_encode(['status' => true, 'msg' => '注册成功', 'data' => $log]);
		}
	}
}
